﻿using System;
using System.Windows.Forms;

namespace svnbox {
    public partial class frmMain : IForm {
        public MenuItem MenuItemMonitor {
            get { return mStartMonitoring; }
        }
        public MenuItem MenuItemStatus {
            get { return mStatus; }
        }
        public NotifyIcon TrayIcon {
            get { return this.trayIcon; }
        }
        public void debug(string s) { System.Diagnostics.Debug.WriteLine("{MainForm}" + s); }

        public frmMain() {
            InitializeComponent();
            mExit.Name = "mExit";
            FormControls.Add(this.mExit);
            mSettings.Name = "mSettings";
            FormControls.Add(this.mSettings);
            mOpenWCPath.Name = "mOpenWCPath";
            FormControls.Add(this.mOpenWCPath);
            mLaunchRepository.Name = "mLaunchRepository";
            FormControls.Add(this.mLaunchRepository);
            mStatus.Name = "mStatus";
            FormControls.Add(this.mStatus);

            App.MainForm = this;
            App.ActivityForm = new frmActivity();
            App.ProfileForm = new frmProfile();

            SendToTray();

            App.Resource.Translate(this);
        }

        public delegate void ProcessNewInstanceDelegate();

        public void ProcessNewInstance() {
            BalloonTipWarning( App.Resource.Lang(this.Name, "msg.NewInstanceNotAllowed" 
                , "Svnbox is already running in background. Cannot start a new instance.") );
        }

        public void BalloonTip(string info) {
            if (App.Profile.Get("global", "desktopnotif") == "1") {
                this.trayIcon.ShowBalloonTip(5000, "Svnbox", info, ToolTipIcon.Info);
            }
        }
        public void BalloonTipWarning(string info) {
            if (App.Profile.Get("global", "desktopnotif") == "1") {
                this.trayIcon.ShowBalloonTip(10000, "Svnbox", info, ToolTipIcon.Warning);
            }
        }

        public void SetTrayIcon(int Status) {
            if (Status == 1)
                this.trayIcon.Icon = Properties.Resources.AppIcon16; //Connected
            else if (Status == 2)
                this.trayIcon.Icon = Properties.Resources.AppIcon16r; //Connecting
            else if(Status == 3)
                this.trayIcon.Icon = Properties.Resources.AppIcon16bw; //Offline

        }

        public void onFormLoad(object sender, EventArgs e) {
            if (App.Profile.Get("global", "startupmonitor") == "1") {
                App.Start();
            }
        }
        
        public void onTray_DoubleClick(object sender, EventArgs e) {
            App.OpenWc();
        }

        private void SendToTray() {
            this.Shown += new EventHandler(delegate(object o,EventArgs e){
                Hide();
            });
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            this.ShowInTaskbar = false;
            this.trayIcon.Visible = true;

            //BalloonTip( App.Resource.Lang(this.Name, "MessageRightClickInfo", "Right click for settings"));
        }

        #region Context Menu Handlers

        public void mSettings_Click(object sender, EventArgs e) {
            App.OpenProfileForm();
        }

        private void mOpenWCPath_Click(object sender, EventArgs e) {
            onTray_DoubleClick(null, null);
        }

        public void mLogger_Click(object sender, EventArgs e) {
            App.OpenActivityForm();
        }


        public void mExit_Click(object sender, EventArgs e) {
            //if (MessageBox.Show(
            //    App.Resource.Lang(this.Name, "MessageConfirmExit", "Are you sure you wan't stop monitoring ?")
            //    , "Confirm exit"
            //    , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {

                this.trayIcon.Visible = false;
                App.Stop();
                Application.ExitThread();
                Application.Exit();
            //}
        }

        private void mLaunchRepository_Click(object sender, EventArgs e) {
            App.OpenRepo();
        }

        private void mStartMonitoring_Click(object sender, EventArgs e) {
            mStartMonitoring.Enabled = false;
            if (App.IsRunning) {
                App.Stop();
            } else {
                App.Start();
            }
        }

        #endregion
    }
}
